package com.zhangh.summer.admin.support.prometheus;

import com.zhangh.summer.admin.support.prometheus.druid.DruidCollector;
import com.zhangh.summer.admin.support.prometheus.redis.RedisCollector;
import com.zhangh.summer.admin.support.prometheus.thread.ThreadCollector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-08-04 17:35
 **/
@Slf4j
@Component("prometheusScheduled")
@DependsOn({"SpringBeanUtil","environment"})
public class PrometheusScheduled  implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private DruidCollector druidCollector;
    @Autowired
    private RedisCollector redisCollector;
    @Autowired
    private ThreadCollector threadCollector;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    druidCollector.execute();
                    redisCollector.execute();
                    threadCollector.execute();
                } catch (Throwable e) {
                    log.error("scheduled run error:{}",e.getMessage());
                    e.printStackTrace();
                }
            }
        }, 1, 15, TimeUnit.SECONDS);
    }
}
